package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strings"
)

func receiveGS(w http.ResponseWriter, r *http.Request) {

	fmt.Println(r.Method, r.URL.Path)
	r.ParseForm()
	if r.Method == "POST" {
		for k, v := range r.Form {
			fmt.Fprintf(w, "%s : %s\r\n", k, v)
			//fmt.Printf("%s : %s\r\n", k, v)
		}
		fmt.Fprintf(w, "OK")
	} else {
		fmt.Fprintf(w, "is GET")
	}
}

func noticeGS(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "notifyGS!")
	v := url.Values{}
	v.Add("tranNo", "tranNo")
	v.Add("uid", "uid")
	v.Add("tran_amount", "tran_amount")
	v.Add("gbpf_gold", "gbpf_gold")
	v.Add("target_gold", "target_gold")
	v.Add("md5key", "md5key")
	postParams := v.Encode()
	io_post_body := strings.NewReader(postParams)
	//"tran_no={$tranNo}&uid={$uid}&tran_amount={$money}&gbpf_gold={$gold}&target_gold={$gamePoint}&md5key={$md5key}
	client := &http.Client{}
	request, err := http.NewRequest("POST", "http://localhost:8888/receiveGS", io_post_body)
	if err != nil {
		log.Fatal("NewRequest err:", err)
	}

	request.Header.Add("Message-Id", "300")
	request.Header.Add("Game-Server-Id", "110")

	response, err := client.Do(request)
	if err != nil {
		log.Fatal("response err:", err)
	}
	defer response.Body.Close()
	//fmt.Println(response)
	bodyByte, _ := ioutil.ReadAll(response.Body)
	body := string(bodyByte)
	fmt.Fprintf(w, body)
}

func main() {
	http.HandleFunc("/", noticeGS)
	http.HandleFunc("/receiveGS", receiveGS)

	if err := http.ListenAndServe(":8888", nil); err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}
